(function($){
    $.extend({
        statistic:function(id,dataX,data,titleX){
            $('#'+id).highcharts({
                chart: {
                    type: 'line'
                },
                title: {
                    text: ''
                },
                subtitle: {
                    text: ''
                },
                xAxis: {
                    title: {
                        text: titleX
                    },
                    categories: dataX,
                    gridLineColor:'#d5e3f3',
                    gridLineWidth:1
                },
                credits:{enabled:false},
                yAxis: {
                    title: {
                        text: '在线用户数 (个)'
                    },
                    gridLineColor:'#d5e3f3',
                    gridLineDashStyle:'longdash',
                    gridLineWidth:1
                },
                tooltip: {
//                    enabled: false,
                    formatter: function() {
                        return '<b>'+ this.series.name +'</b><br>'+this.x +': '+ this.y +'个';
                    },
                    style: {
                        color: '#4d759e',
                        fontSize: '12px',
                        padding: '8px'
                    }
                },
                legend: {
                    floating: 'true',
                    align: 'left',
                    verticalAlign: 'bottom',
                    borderWidth: 0
                },
                series: [{
                    name: '在线用户统计',
                    data: data
                }]
            });
        }
    })
})(jQuery)
var statisticalData={
    init:function(){
        var self=this;
        self.shadeDiv = $("#shade-div");
        self.bindEvents();

    },
    bindEvents:function(){
        var self=this;
        self.ajaxPOST(ctx+"/statistical/online_user!chartData1.xhtml",self.chartData);
        self.ajaxPOST(ctx+"/statistical/online_user!chartData2.xhtml",self.chartData1);
        $("#button1").click(function(){
        	statisticalData.ajaxPOST(ctx+"/statistical/online_user!chartData1.xhtml",function(data){
        		var date=$("#date").val().split("-");
        		$("#spanyear1").html(date[0]);
        		$("#spanmonth1").html(date[1]);
        		$("#spanday1").html(date[2]);
        		$("#max1").html(data.max);
        		statisticalData.chartData(data);
        	},$("#form11").serialize());
        });
        $("#button2").click(function(){
        	statisticalData.ajaxPOST(ctx+"/statistical/online_user!chartData2.xhtml",function(data){
        		$("#spanyear2").html($("#year").val());
        		$("#spanmonth2").html($("#month").val());
        		$("#spanhour2").html($("#hour").val());
        		$("#max2").html(data.max);
        		statisticalData.chartData1(data);
        	},$("#form22").serialize());
        });
        
    },
//    按日期查询
    chartData:function(data){
    	$("#max1").html(data.max);
        $.statistic("container1",data.data_X1,data.data_Y1,data.title_X1);
    },
//    按时间查询
    chartData1:function(data){
    	$("#max2").html(data.max);
        $.statistic("container2",data.data_X2,data.data_Y2,data.title_X2);
    },
    ajaxGET:function (url, fn,data,beforeSendFn) {
        var beforeSendFn = beforeSendFn || function(){};
        $.ajax({type:"GET", dataType:'json', context:this, url:url, data:data, success:fn, beforeSend:beforeSendFn});
    },
    ajaxPOST:function (url, fn,data,beforeSendFn) {
        var beforeSendFn = beforeSendFn || function(){};
        $.ajax({type:"POST", dataType:'json', context:this, url:url, data:data, success:fn, beforeSend:beforeSendFn});
    }

}
$(function(){
    statisticalData.init()
});

var _id=0;
var settingMsg = {
			view: {
				dblClickExpand: false
			},
			data: {
				simpleData: {
					enable: true
				}
			},
			callback: {
				beforeClick: beforeClick,
				onClick: onClick
			}
		};

function beforeClick(treeId, treeNode) {
	var check = treeNode&&(treeNode.id==0||treeNode.level==2);
	if(check){
			hideMenu();
	}else{
		common.alert("只能选择二级部门");
	}
	return check;
}

function onClick(e, treeId, treeNode) {
	var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
	nodes = zTree.getSelectedNodes(),
	v = "";
	nodes.sort(function compare(a,b){return a.id-b.id;});
	for (var i=0, l=nodes.length; i<l; i++) {
		v += nodes[i].name + ",";
	}
	if (v.length > 0 ) v = v.substring(0, v.length-1);
	var cityObj = $("#citySel"+_id);
	cityObj.attr("value", v);
	if(treeNode.id==0){
		$("#dept"+_id).val("");
	}else{
		$("#dept"+_id).val(treeNode.id);
	}
}

		function showMenu(id) {
			_id=id;
			var cityObj = $("#citySel"+id);
			var cityOffset = cityObj.offset();
			$("#menuContent").css({left:cityOffset.left + "px", top:cityOffset.top + cityObj.outerHeight() + "px"}).slideDown("fast");

			$("body").bind("mousedown", onBodyDown);
		}
		function hideMenu() {
			$("#menuContent").fadeOut("fast");
			$("body").unbind("mousedown", onBodyDown);
		}
		function onBodyDown(event) {
			if (!(event.target.id == "menuBtn" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length>0)) {
				hideMenu();
			}
		}
	statisticalData.ajaxGET(ctx+"/statistical/release_msg!getDeptTree.xhtml",function(data){
		$.fn.zTree.init($("#treeDemo"), settingMsg, data);
	},"");